import Global from "@/Global";
import md5 from "js-md5";

import { PersonLoginedGetNickName, CompanyGetCompanyName } from "@/util/http";
import { returnToPhoneWithData } from "@/util/phone";
import { checkPersonLogin, checkCompanyLogin } from "@/util/session";

let appkey = "fd41fcc0400f66304a4d630b";
let timestamp = new Date().getTime();
let random_str = "ie1r9flfvtrppyuq8p4tf9qtu";
let signature = md5(
	`appkey=${appkey}&timestamp=${timestamp}&random_str=${random_str}&key=93ca8ae71b15dd6712397ea3`
);

// // eslint-disable-next-line no-undef
// let JIM = new JMessage({ debug: false });
// JIM.init({
// 	appkey,
// 	random_str,
// 	signature,
// 	timestamp,
// })
// 	.onSuccess(function (data) {
// 		//data.code 返回码
// 		//data.message 描述
// 		console.log("init onSuccess", data);

// 		JIM.login({
// 			username: "C51543",
// 			password: "123456",
// 		})
// 			.onSuccess(function (data) {
// 				//data.code 返回码
// 				//data.message 描述
// 				//data.online_list[] 在线设备列表
// 				//data.online_list[].platform  Android,ios,pc,web
// 				//data.online_list[].mtime 最近一次登录时间
// 				//data.online_list[].isOnline 是否在线 true or false
// 				//data.online_list[].isLogin 是否登录 true or false
// 				//data.online_list[].flag 该设备是否被当前登录设备踢出 true or false
// 				console.log("login onSuccess", data);
// 				JIM.onMsgReceive(function (data) {
// 					// data.messages[]
// 					// data.messages[].ctime_ms
// 					// data.messages[].msg_type 会话类型
// 					// data.messages[].msg_id
// 					// data.messages[].from_appey 单聊有效
// 					// data.messages[].from_username 单聊有效
// 					// data.messages[].from_gid 群聊有效
// 					// data.messages[].need_receipt
// 					// data.messages[].content
// 					// data.messages[].custom_notification.enabled
// 					// data.messages[].custom_notification.title
// 					// data.messages[].custom_notification.alert
// 					// data.messages[].custom_notification.at_prefix
// 					console.log("收到新消息", data);
// 					//   Toast(`收到新消息【${data.messages[0].content.msg_body.text}】`);
// 				});
// 			})
// 			.onFail(function (data) {
// 				//同上
// 			});
// 	})
// 	.onFail(function (data) {
// 		// 同上
// 		console.log(data);
// 	});

let person_JIM;
export function onPersonIM(personid, vm) {
	let appkey = "fd41fcc0400f66304a4d630b";
	let timestamp = new Date().getTime();
	let random_str = "ie1r9flfvtrppyuq8p4tf9qtu";
	let signature = md5(
		`appkey=${appkey}&timestamp=${timestamp}&random_str=${random_str}&key=4fa1cf2e68fb3e5243f0ca74`
	);

	// eslint-disable-next-line no-undef
	person_JIM = new JMessage({ debug: false });
	person_JIM.init({
		appkey,
		random_str,
		signature,
		timestamp,
	})
		.onSuccess(function (data) {
			if (data.code === 0) {
				onPersonLogin(personid, vm);
			}
		});
}

//个人登录
function onPersonLogin(personid, vm) {
	person_JIM
		.login({
			username: personid,
			password: "123456",
		})
		.onSuccess(function (data) {
			onPersonUpdateInfo();
			onPersonMsgReceive(vm, personid);
		})
		.onFail(function (data) {
			if (data.code === 880103) {
				//用户未注册
				onPersonReg(personid, vm);
			} else if (data.code === 880107) {
				//已经登录
				onPersonMsgReceive(vm, personid);
			}
		});
}

function onPersonReg(personid, vm) {
	if (personid.indexOf("C") >= 0) {
		CompanyGetCompanyName().then((res) => {
			// console.log(res.name);
			person_JIM
				.register({
					username: personid,
					password: "123456",
					nickname: res.name,
				})
				.onSuccess(function (data) {
					console.log(data);
					onPersonLogin(personid, vm);
				})
				.onFail(function (data) {
					console.log(data);
				});
		});
	} else {
		PersonLoginedGetNickName().then((res) => {
			person_JIM
				.register({
					username: personid,
					password: "123456",
					nickname: res,
				})
				.onSuccess(function (data) {
					console.log(data);
					onPersonLogin(personid, vm);
				})
				.onFail(function (data) {
					console.log(data);
				});
		});
	}
}

function onPersonUpdateInfo() {
	if (checkPersonLogin()) {
		PersonLoginedGetNickName().then((res) => {
			person_JIM
				.updateSelfInfo({
					nickname: res,
				})
				.onSuccess(function (data) {
					// console.log("更新信息", data);
				});
		});
	} else if (checkCompanyLogin()) {
		CompanyGetCompanyName().then((res) => {
			person_JIM
				.updateSelfInfo({
					nickname: res.name,
				})
				.onSuccess(function (data) {
					// console.log("更新信息", data);
				});
		});
	}
}

function onPersonMsgReceive(vm, personid) {
	console.log("onPersonMsgReceive");
	try {
		returnToPhoneWithData({ action: "person_im_login", personid });
	} catch (error) {
		console.log("onPersonMsgReceive_error", error);
	}
	person_JIM.onMsgReceive(function (data) {
		let im_data = {
			fromUserid: data.messages[0].content.from_id,
			message: data.messages[0].content.msg_body.text,
		};
		vm.$bus.$emit(Global.bus_person_im, im_data);
	});
}

export function onPersonSendMessage(companyid, message) {
	person_JIM
		.sendSingleMsg({
			target_username: companyid,
			content: message,
			// appkey: "20dab7dc480aba4a54383fed",
		})
		.onSuccess(function (data) {
			//console.log(data);
		})
		.onFail(function (data) {
			console.log(data);
		});
}
//退出
export function onIMLogOut() {
	person_JIM.loginOut();
	try {
		returnToPhoneWithData({ action: "im_loginOut" });
	} catch (error) {
		//
	}
}
